maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=flux
pkgver=2.4.0
pkgrel=0
pkgdesc="Open and extensible continuous delivery solution for Kubernetes"
url="https://fluxcd.io/"
# blocked by kubernetes
arch="all !armhf !riscv64 !loongarch64"
license="Apache-2.0"
makedepends="go bash kustomize"
checkdepends="etcd kube-apiserver"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/fluxcd/flux2/archive/v$pkgver/flux-$pkgver.tar.gz"
builddir="$srcdir/flux2-$pkgver"
options="net" # download Go modules

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# secfixes:
#   0.36.0-r0:
#     - CVE-2022-39272

build() {
	bash ./manifests/scripts/bundle.sh

	go build -v \
		-ldflags "-X main.VERSION=$pkgver" \
		./cmd/flux

	for shell in bash fish zsh; do
		./$pkgname completion $shell > $pkgname.$shell
	done
}

check() {
	case "$CARCH" in
		# etcd defines only these architectures as supported
		# Ref: https://github.com/etcd-io/etcd/blob/ac3d5d77ea5fdbc12ef07a6f6fe1722f06d75b24/server/etcdmain/etcd.go#L264
		x86_64|aarch64|ppc64le|s390x) ;;
		*)
			# shellcheck disable=2155
			export ETCD_UNSUPPORTED_ARCH="$(go env GOARCH)" ;;
	esac

	# e2e tests are slow, and require an actual Kubernetes cluster
	KUBEBUILDER_ASSETS=/usr/bin \
		KUBEBUILDER_ATTACH_CONTROL_PLANE_OUTPUT=true \
		go test -tags=unit ./...
}

package() {
	install -Dm755 $pkgname -t "$pkgdir"/usr/bin/

	install -Dm644 $pkgname.bash \
		"$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="
bc5020300d3607d50ef6ef9223be98684df1b296c64a25b0cfc35577b73eda79f52514af4b77904337125d85c1a89178e766a8a978d46929fb36a63be0203ee0  flux-2.4.0.tar.gz
"
